package com.lczyfz.istep.modules.openstack.api.neutron.impl;

import com.lczyfz.istep.modules.openstack.api.neutron.impl.parseJson.NeutronParse;
import com.lczyfz.istep.modules.openstack.api.neutron.interfaces.Ports;
import com.lczyfz.istep.modules.openstack.api.neutron.model.Port;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.List;

public class PortImpl implements Ports {
	public RequestFactory factory = new RequestFactory("neutron");
	
	@Override
	public List<Port> getPorts(){
		List<Port> ports = null;
		Request request = factory.getRequest();
		request.setUrl("/v2.0/ports");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);
		
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		ports = NeutronParse.resolvePorts(body);
		return ports;
	}
	
	@Override
	public Port createPort(Port port){
		Port ports = new Port();
		JSONObject params = NeutronParse.packagePort(port);
		
		Request request = factory.getRequest();
		request.setUrl("/v2.0/ports");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_CREATED!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		ports = NeutronParse.resolveCreatePort(res); 
		return ports;
	}
	
	@Override
	public void deletePort(String portId) {
		Request request = factory.getRequest();
		request.setUrl("/v2.0/ports/" + portId);
		request.setRequestType(RequestType.DELETE);
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_SUCCESS != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}

	public static void main(String[] args) {
		
		PortImpl portdemo = new PortImpl();
		System.out.println("�˿���Ϣ");
		List<Port> ports = portdemo.getPorts();
//		for(Port port : ports){
//			System.out.println(port.getPortId() + " ," + port.getPortName() + " ,"+ port.getPortNetworkId());
//		}
//
//		System.out.println("创建端口");
//		Port newPort = new Port();
//		newPort.setPortName("newPort1");
//		newPort.setPortNetworkId("2fa6dbdc-c05e-45c8-b9fc-4ae0e8fb0552");
//		JSONArray fixedIps = new JSONArray();
//		JSONObject fixedIp = new JSONObject();
//		fixedIp.put("ip_address", "10.0.0.6");
//		fixedIps.add(0,fixedIp);
//		newPort.setPortFixedIp(fixedIps);
//
//		Port getPort = portdemo.createPort(newPort);

		ports = portdemo.getPorts();
		for(Port port : ports){
			System.out.println(port.getPortId() + " ," + port.getPortName() + " ,"+ port.getPortNetworkId());
		}

//		System.out.println(getPort.getPortId() + " " + getPort.getPortName() + " " + getPort.getPortNetworkId() + " " + getPort.getPortFixedIp());;

//		System.out.println("ɾ���˿�");
//		portdemo.deletePort("5624d69b-e0fd-4fab-ba18-9b4a94aa86cd");
//
		
	}

}
